package com.hongyuan.sellcoach.ui.member.activity;

import android.os.Bundle;
import android.widget.Button;

import androidx.lifecycle.Observer;
import androidx.lifecycle.ViewModelProviders;

import com.google.android.material.tabs.TabLayoutMediator;
import com.hongyuan.sellcoach.R;
import com.hongyuan.sellcoach.base.AppViewModelFactory;
import com.hongyuan.sellcoach.base.CustomActivity;
import com.hongyuan.sellcoach.base.CustomFragment;
import com.hongyuan.sellcoach.data.Constants;
import com.hongyuan.sellcoach.databinding.ActivityMemberDetailsBinding;
import com.hongyuan.sellcoach.ui.Page2Adapter;
import com.hongyuan.sellcoach.ui.member.fragment.AdmissionRecordFragment;
import com.hongyuan.sellcoach.ui.member.fragment.CourseRecordFragment;
import com.hongyuan.sellcoach.ui.member.fragment.MemberCourseFragment;
import com.hongyuan.sellcoach.ui.member.fragment.MemberInformationFragment;
import com.hongyuan.sellcoach.ui.member.view_mode.MemberDetailsViewModel;
import java.util.ArrayList;
import java.util.List;

import me.tatarka.bindingcollectionadapter2.BR;

public class MemberDetailsActivity extends CustomActivity<ActivityMemberDetailsBinding, MemberDetailsViewModel> {

    private List<CustomFragment> fragments;

    private String xy_mid;

    //基础信息fragment
    private CustomFragment infoFragment;

    @Override
    public int initContentView(Bundle savedInstanceState) {
        return R.layout.activity_member_details;
    }

    @Override
    public int initVariableId() {
        return BR.viewModel;
    }

    @Override
    public MemberDetailsViewModel initViewModel() {
        AppViewModelFactory factory = AppViewModelFactory.getInstance(getApplication());
        return ViewModelProviders.of(this, factory).get(MemberDetailsViewModel.class);
    }

    @Override
    public void initParam() {
        super.initParam();

        xy_mid = getBundle().getString("xy_mid");
    }

    @Override
    public void initView() {
        super.initView();

        binding.comBack.setOnClickListener(view -> finish());

        //初始化数据
        fragments = new ArrayList<>();
        infoFragment = new MemberInformationFragment().setTabTag("基本信息");

        fragments.add(infoFragment);
        fragments.add(new MemberCourseFragment().setTabTag("私教课信息"));
        fragments.add(new AdmissionRecordFragment().setTabTag("入场记录"));
        fragments.add(new CourseRecordFragment().setTabTag("上课记录"));

        binding.mViewPager.setAdapter(new Page2Adapter(this,fragments));
        TabLayoutMediator tabLayoutMediator = new TabLayoutMediator(binding.tab, binding.mViewPager, (tab, position) -> {
            CustomFragment f = fragments.get(position);
            tab.setText(f.getTabTag());
        });
        tabLayoutMediator.attach();
    }

    @Override
    public void initData() {
        super.initData();

        viewModel.setParams("xy_mid",xy_mid)
                .requestData(Constants.MEMBER_INFO);
    }

    @Override
    public void initViewObservable() {
        super.initViewObservable();

        viewModel.uc.setInfo.observe(this, aVoid -> {
            MemberInformationFragment informationFragment = (MemberInformationFragment)infoFragment;
            informationFragment.setInfoData(viewModel.info.get());
        });
    }
}
